#!/bin/bash
rpath="$(readlink ${BASH_SOURCE})"
if [ -z "$rpath" ];then
    rpath=${BASH_SOURCE}
fi
thisDir="$(cd $(dirname $rpath) && pwd)"
cd "$thisDir"

user="${SUDO_USER:-$(whoami)}"
home="$(eval echo ~$user)"

red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
cyan=$(tput setaf 5)
bold=$(tput bold)
reset=$(tput sgr0)
function runAsRoot(){
    verbose=0
    while getopts ":v" opt;do
        case "$opt" in
            v)
                verbose=1
                ;;
            \?)
                echo "Unknown option: \"$OPTARG\""
                exit 1
                ;;
        esac
    done
    shift $((OPTIND-1))
    cmd="$@"
    if [ -z "$cmd" ];then
        echo "${red}Need cmd${reset}"
        exit 1
    fi

    if [ "$verbose" -eq 1 ];then
        echo "run cmd:\"${red}$cmd${reset}\" as root."
    fi

    if (($EUID==0));then
        sh -c "$cmd"
    else
        if ! command -v sudo >/dev/null 2>&1;then
            echo "Need sudo cmd"
            exit 1
        fi
        sudo sh -c "$cmd"
    fi
}
###############################################################################
# write your code below (just define function[s])
# function with 'function' is hidden when run help, without 'function' is show
###############################################################################
# TODO
install(){
ROOTID=0
if [ $EUID -eq $ROOTID ];then
    echo "Don't run as root!"
    exit 1
fi

#1. detect ev INIT_HTTP_PROXY to set http_proxy https_proxy HTTP_PROXY HTTPS_PROXY git-proxy
if [ -z "$INIT_HTTP_PROXY" ];then
    echo "\$INIT_HTTP_PROXY is empty!"
    echo "Enter http proxy if you need(sample format: http://<host>:<port>)"
    echo -n "(press Enter to disable proxy): "
    read INIT_HTTP_PROXY
fi
if [ -n "$INIT_HTTP_PROXY" ];then
    echo "\$INIT_HTTP_PROXY is $INIT_HTTP_PROXY,assign it to http_proxy https_proxy git-proxy"
    export http_proxy=$INIT_HTTP_PROXY
    export https_proxy=$INIT_HTTP_PROXY
    export HTTP_PROXY=$INIT_HTTP_PROXY
    export HTTPS_PROXY=$INIT_HTTP_PROXY
    git config --global http.proxy $INIT_HTTP_PROXY
    git config --global https.proxy $INIT_HTTP_PROXY
else
    echo "No use proxy"
fi
#2. mkdir /usr/local/bin
if [ ! -d /usr/local/bin ];then
    echo "mkdir /usr/local/bin"
    runAsRoot "mkdir /usr/local/bin"
fi
if [ ! -d $home/.bin ];then
    echo "mkdir $home/.bin"
    mkdir $home/.bin
fi
#TODO unset proxy

#4. install homebrew(--needed)
if ! command -v brew >/dev/null 2>&1;then
    echo "install homebrew..."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
fi
#5. mac app store 所有源 
echo "allow App Store all source..."
runAsRoot "spctl --master-disable"
#6. brew install: vim nvim coreutils findutils grep gnu-tar gnu-sed gawk gnutls gnu-indent gnu-getopt gnu-which wget python3 tree go pv
apps="bash coreutils findutils grep gnu-tar gnu-sed gawk gnutls gnu-indent gnu-getopt gnu-which wget python3 tree go pv fd bat"
for app in $apps;do
    if ! brew list $app >/dev/null 2>&1;then
        echo "Install $app..."
        brew install $app
    fi
done
}



###############################################################################
# write your code above
###############################################################################
function help(){
    cat<<EOF2
Usage: $(basename $0) ${bold}CMD${reset}

${bold}CMD${reset}:
EOF2
    perl -lne 'print "\t$1" if /^\s*(\w+)\(\)\{$/' $(basename ${BASH_SOURCE}) | grep -v runAsRoot
}

case "$1" in
     ""|-h|--help|help)
        help
        ;;
    *)
        "$@"
esac
